package com.yongdin.minimalism;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android_serialport_api.SerialPortDevice;

public class SerialPortIOManage {
    SerialPortDevice temPortDevice0 = null;
    private InputStream mInputStream0 = null;
    private OutputStream mOutputStream0 = null;


    ReadThread0 mReadThread0 = null;

    onSerialPortIOListener mListener = null;

    private static class SingletonHolder {
         private static final SerialPortIOManage INSTANCE = new SerialPortIOManage();
    }

    private SerialPortIOManage() {

    }

    public static final SerialPortIOManage getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void setonSerialPortIOListener(onSerialPortIOListener listener) {
        mListener = listener;
    }

    public interface onSerialPortIOListener {
         void OnConnectStatusCallBack(boolean statue);

         void OnIOCallBack(int which, byte[] data, int length);
    }

    public void connect(String path0, int baudrate) {

        temPortDevice0 = new SerialPortDevice(path0, baudrate, 0);

        if (temPortDevice0.connect()) {
            mInputStream0 = temPortDevice0.getInputStream();
            mOutputStream0 = temPortDevice0.getOutputStream();
            mReadThread0 = new ReadThread0();
            mReadThread0.start();
        }
    }

    public InputStream getmInputStream0() {
        if (mInputStream0 != null) {
            return mInputStream0;
        }
        return null;
    }

    public OutputStream getmOutputStream0() {
        if (mOutputStream0 != null) {
            return mOutputStream0;
        }
        return null;
    }




    public void disConnect() {

        if (mReadThread0 != null) {
            mReadThread0.interrupt();
            mReadThread0 = null;
        }
        try {
            if (mInputStream0 != null) {
                mInputStream0.close();
                mInputStream0 = null;
            }
            if (mOutputStream0 != null) {
                mOutputStream0.close();
                mOutputStream0 = null;

                if (temPortDevice0 != null) {
                    temPortDevice0.close();
                }

            }

        } catch (IOException e) {
            e.printStackTrace();
        }



    }

    byte[] sendbytes0 = null;

    //写数据
    public void sendbytes(byte[] bb) {
        if (mOutputStream0 != null) {
            try {
                mOutputStream0.write(bb);
                mOutputStream0.write('\n');
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    class ReadThread0 extends Thread {
        public void run() {
            int nMaxBufLength = 1024;
            byte[] buffer = new byte[nMaxBufLength];
            while (!isInterrupted()) {
           // while (true) {

                try {
                    int byteRead = -1;
                    if (mInputStream0 != null) {
                        byteRead = mInputStream0.read(buffer);
                        if (byteRead >= 0) {
                            if (mListener != null) {
                                sendbytes0 = new byte[byteRead];
                                System.arraycopy(buffer, 0, sendbytes0, 0, byteRead);

                                mListener.OnIOCallBack(0, sendbytes0, byteRead);
                            }
                        }
                    } else {
                        continue;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    continue;
                }
            }//while(!isInterrupted())
        }
    }
}
